package com.kkwrite.regimen.consumer.service.address;

import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogInDTO;
import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogOutDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by lisha on 2018/7/13 9:30.
 *
 * @author lisha
 */
@Service("positionService")
public class PositionServiceImpl implements PositionService {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public RegPositionLogOutDTO saveOrUpdate(RegPositionLogInDTO inDTO, String token) {
        Object userIdObj = stringRedisTemplate.opsForHash().get(token, "userId");
        if (userIdObj == null) {
            return new RegPositionLogOutDTO("false", "002");
        }
        inDTO.setUserId(Integer.valueOf((String) userIdObj));
        return restTemplate.postForObject(Constant.SERVICE_PREFIX + "/position/saveOrUpdate", inDTO, RegPositionLogOutDTO.class);
    }
}
